package com.dirox.confluence.labelmanagement.utils;

import com.atlassian.mail.Email;
import com.atlassian.mail.MailException;
import com.atlassian.mail.MailFactory;
import com.atlassian.mail.server.SMTPMailServer;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class MailHelper {
    private static MailHelper instance = null;
    public SMTPMailServer server = null;

    private MailHelper() throws MailException {
        server = MailFactory.getServerManager().getDefaultSMTPMailServer();
    }

    private static MailHelper getInstance() throws MailException {
        if (MailHelper.instance == null) {
            MailHelper.instance = new MailHelper();
        }

        return MailHelper.instance;
    }

    public static void setPrefix(String prefix) throws MailException {
        if (getInstance().server != null) {
            getInstance().server.setPrefix(prefix);
        }
    }

    public static String getDefaultFrom() throws MailException {
        if (getInstance().server != null) {
            return getInstance().server.getDefaultFrom();
        } else {
            return "";
        }
    }

    public static boolean send(Email email) {
        try {
            if (getInstance().server != null) {
                getInstance().server.send(email);

                return true;
            }
        } catch (MailException e) {
            // do nothing
        }

        return false;
    }

    public static boolean send(String from, String to, String subject,
        String body) {
        Email email = new Email(to);
        email.setFrom(from);
        email.setSubject(subject);
        email.setMimeType("text/html");
        email.setBody(body);

        return send(email);
    }

    public static boolean send(String to, String subject, String body)
        throws MailException {
        return send(getDefaultFrom(), to, subject, body);
    }
    
    public static void sendFromGmail(String to, String subject, String content) {
		final String username = "atlas.dirox.team.2012@gmail.com";
		final String password = "dirox2012";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(
				props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("atlas.dirox.@gmail.com"));
			message.setRecipients(
				Message.RecipientType.TO,
				InternetAddress.parse(to));
			message.setSubject(subject);
			message.setText(content);

			Transport.send(message);
			System.out.println("Sent Mail");
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}
